import org.openjdk.jol.info.ClassLayout;

/**
 * syn阻塞 --》 cxq队列
 * wait --》 waitset
 * notify --》 waitset --》entrylist（不为空方cxq）
 * 退出syn --》 entrylist唤醒，为空就从cxq唤醒
 *
 *
 */
public class SynchronizedWaitNotifyDemo {
    public static void main(String[] args) {
        Object lock = new Object();

        Thread t1 = new Thread(() -> {
            System.out.println("Thread 1 start!!!!!!");
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (Exception e) {
                }
                System.out.println("Thread 1 end!!!!!!");
            }
        });
        Thread t2 = new Thread(() -> {
            System.out.println("Thread 2 start!!!!!!");
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (Exception e) {
                }
                System.out.println("Thread 2 end!!!!!!");
            }
        });
        Thread t3 = new Thread(() -> {
            System.out.println("Thread 3 start!!!!!!");
            synchronized (lock) {
                try {
                    lock.wait();
                } catch (Exception e) {
                }
                System.out.println("Thread 3 end!!!!!!");
            }
        });
        Thread t4 = new Thread(() -> {
            System.out.println("Thread 4 start!!!!!!");
            synchronized (lock) {
                try {
                    System.in.read();
                } catch (Exception e) {
                }
                lock.notify();
                lock.notify();
                lock.notify();
                System.out.println("Thread 4 end!!!!!!");
            }
        });
        Thread t5 = new Thread(() -> {
            System.out.println("Thread 5 start!!!!!!");
            synchronized (lock) {
                System.out.println("Thread 5 end!!!!!!");
            }
        });
        Thread t6 = new Thread(() -> {
            System.out.println("Thread 6 start!!!!!!");
            synchronized (lock) {
                System.out.println("Thread 6 end!!!!!!");
            }
        });
        Thread t7 = new Thread(() -> {
            System.out.println("Thread 7 start!!!!!!");
            synchronized (lock) {
                System.out.println("Thread 7 end!!!!!!");
            }
        });
        t1.start();
        sleep_1_second();

        t2.start();
        sleep_1_second();

        t3.start();
        sleep_1_second();

        t4.start();
        sleep_1_second();

        t5.start();
        sleep_1_second();

        t6.start();
        sleep_1_second();

        t7.start();

    }

    private static void sleep_1_second() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class A{}
}
